/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlysieuthi;

import DAO.*;
import DTO.*;
import Model.CModel;
import java.awt.event.ItemEvent;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import Model.ChiTietKhuyenMai;
public class FrmHoaDonBH extends javax.swing.JFrame {

    /**
     * Creates new form FrmHoaDonBH
     */
    public FrmHoaDonBH(FrmMenu _menu) {
        initComponents();
        this.dtcNgayBan.setDateFormatString(QuanLySieuThi.formatDate);        
        LoadComBoBox();        
        this.isSetEnabled(true);
        loadKhuyenMai();
        this._menu = _menu;
    }
    FrmMenu _menu = null;
    DecimalFormat df = new DecimalFormat("#,###");                    
    List<CChiTietHoaDon> listChiTiet = new ArrayList<CChiTietHoaDon>();
    CHoaDon hoadon = new CHoaDon();
    CHoaDon hdCurrent = null;
    public Boolean add = false;
    public Boolean edit = false;
    public Boolean delete = false;
    CHoaDonDAO hoadonDao = new CHoaDonDAO();
    CChiTietHoaDonDAO chitietDAO =new CChiTietHoaDonDAO();
    CTonKhoDAO tonkhoDAO = new CTonKhoDAO();
    List<CTon_Kho> listTonKho = new ArrayList<CTon_Kho>();
    //CSanPham sanpham = new CSanPham();
    CSanPhamDAO sanphamDAO = new CSanPhamDAO();
    List<CSanPham> listSanPham = new ArrayList<CSanPham>();
    SubConfig config = SubConfig.getInstance();  
    CKho khoCurrent = new CKho();
    CKhachHang khachHang = new CKhachHang();
    List<CKho> listKho = new ArrayList<CKho>();
    List<CKhachHang> lKhachHang = new ArrayList<CKhachHang>();
    CKhachHangDAO khachhangDAO = new CKhachHangDAO();
    CKho cuaHangCurr = new CKho();
    CSanPham sanphamCurrent = new CSanPham();
    String phieu ="HDBH";
    List<CTon_Kho> l_tk_CuaHang = new ArrayList<CTon_Kho>();
    List<CGiaBan>lGiaBan = new ArrayList<CGiaBan>();
    CKhachHang khCurrent = null;
    List<CNhomKhachHang> lNhom_kh = new ArrayList<CNhomKhachHang>();
    List<CChiTietHoaDon> lcthd = new ArrayList<CChiTietHoaDon>();
    List<CHoaDon>lhoa_don = new ArrayList<CHoaDon>();
    int chiet_khau = 0;
    int currentIndex = 0;// lưu vị trị hiện tại của dòng
    int numberRow = 0;//tổng số dòng của hóa đơn
    long id = -1;
    List<CNhanVien> lnhanvien = new ArrayList<CNhanVien>();
    List<CTon_Kho> lkho_xuat = new ArrayList<CTon_Kho>();
    double don_gia_von = 0.0;
    HashMap giam_gia = new HashMap();
    HashMap mua_N_tangM = new HashMap();
    HashMap mua_N_giamM = new HashMap();
    HashMap mua_NtangMk = new HashMap();
    List<CChiTietHoaDon> lsan_pham_tang = new ArrayList<CChiTietHoaDon>();
    List<CChiTietHoaDon> lShowcthdbh = new ArrayList<CChiTietHoaDon>();
    List<CTinhTrang> ltinh_trang = new ArrayList<CTinhTrang>();
    private void loadKhuyenMai(){
        giam_gia = new HashMap();
        mua_N_tangM = new HashMap();
        mua_N_giamM = new HashMap();
        mua_NtangMk = new HashMap();
        CModel _model = new CModel(QuanLySieuThi.user_name);
        _model.getChuongTrinhKH(giam_gia, mua_N_tangM, mua_N_giamM, mua_NtangMk);
        _model.Close();
    } 
    
    private Boolean isExists(List<CChiTietHoaDon> list,List<ChiTietKhuyenMai> in_tems){
        Boolean flag = false;
        for(ChiTietKhuyenMai item : in_tems){            
            for(CChiTietHoaDon in_cthd : list){
                if(item.getMhmId() == in_cthd.getSanPhamId() && item.getSoLuongMua() <= in_cthd.getSoLuong()){
                    flag = true;
                    break;
                }
            }
            if(flag == false){
                return false;
            }
        }
        return true;
    }
    
    private Boolean isExists(List<CChiTietHoaDon> list,long san_pham_id){        
        for(CChiTietHoaDon item : list){
            if(item.getSanPhamId() == san_pham_id){
                return true;
            }
        }
        return false;
    }
    
    private void getKhuyenMai(List<CChiTietHoaDon> list){
        this.setKhuyenMai(list, mua_N_tangM, "loai2");
        this.setKhuyenMai(list, mua_NtangMk, "loai4");       
    }
    
    //giam gia chi khi nao mua duoc n san pham moi giam gia
    private void setGiamGiaMuaNGiamM(List<CChiTietHoaDon> list){
        /// mua n san pham se giam gia m%
        if(mua_N_giamM.size() > 0){
            
        }
    }
    ///giam gia cho tung san pham hi mua trong thoi gian khuyen mai
    private void setGiamGiaKhuyenMai(List<CChiTietHoaDon> list){
        if(this.giam_gia.size() > 0){
            List<ChiTietKhuyenMai> lchitietkm = new ArrayList<ChiTietKhuyenMai>();
            int index = 0;
            for(int i = 0;i < giam_gia.size();i++){
                index++;
                String key = "loai1" + String.format("%s", index);
                lchitietkm =(List<ChiTietKhuyenMai>)giam_gia.get(key);
                for(CChiTietHoaDon item : list){
                    for(ChiTietKhuyenMai km : lchitietkm){
                        if(item.getSanPhamId() == km.getMhmId() && item.getSoLuong() >= km.getSoLuongMua()){
                            double _gg = km.getGiamgia();
                            double _tiengiam = _gg* item.getSoLuong()*item.getDongia()/100;
                            item.setGiamGia(_tiengiam);
                            break;
                        }
                    }
                }
            }
        }
    }
    //// mua san pham tang san pham cung loai hoac khac loi
    private void setKhuyenMai(List<CChiTietHoaDon> list,HashMap hm,String key){
        if(hm.size() > 0){
            int index = 0;
            List<ChiTietKhuyenMai> lchitietkm = new ArrayList<ChiTietKhuyenMai>();
            for(int i = 0; i < hm.size();i++){
                index++;
                String _key = key + String.format("%s", index);
                lchitietkm =(List<ChiTietKhuyenMai>)hm.get(_key);
                if(this.isExists(list, lchitietkm)){
                    for(ChiTietKhuyenMai item : lchitietkm){
                        if(item.getMhtId() != -1){
                            CChiTietHoaDon _cthd = new CChiTietHoaDon();
                            _cthd.setSanPhamId(item.getMhtId());
                            _cthd.setTenSanPham(item.getTenSanPham());
                            _cthd.setDongia(0.0);
                            _cthd.setGiaVon(0.0);
                            _cthd.setTang(true);
                            _cthd.setSoLuong(item.getSoLuongTang());
                            _cthd.setThanhTien(0.0);
                            _cthd.setTienTra(0.0);
                            _cthd.setTienThanhToan(0.0);
                            if(!this.isExists(lsan_pham_tang, id)){
                                lsan_pham_tang.add(_cthd);
                                lShowcthdbh.add(_cthd);
                            }
                        }
                    }
                }
            }
        }        
    }
    
    private void defaultValue(){
        dtcNgayBan.setDate(new Date());        
        txtNVNhap.setText(QuanLySieuThi.ho_ten);
        txtTongTien.setText("0.0");
        this.cbbKho.setSelectedIndex(-1);
        this.cbbKhachHang.setSelectedIndex(-1);
        this.cbbnvbanhang.setSelectedIndex(-1);
    }
    
    private int indexKhachHang(List<CKhachHang> khs,long kh_id){
        int index = 0;
        for(CKhachHang item : khs){
            if(item.getId() == kh_id){
                return index;
            }
            index++;
        }
        return -1;
    }
    
    private void LoadComBoBox(){
        config.init();
        listSanPham = sanphamDAO.getAll(config);
        tonkhoDAO.sWhere = "  where " + String.format("thang = %s and nam = %s", QuanLySieuThi.thang,QuanLySieuThi.nam);
        listTonKho = tonkhoDAO.getAll(config);  
        khachhangDAO.sWhere = " where loai = 1";
        lKhachHang = khachhangDAO.getAll(config);
        CKhoDAO khoDAO = new CKhoDAO();
        khoDAO.sWhere = " where cua_hang = true";
        listKho = khoDAO.getAll(config);
        CGiaBanDAO giabanDAO = new CGiaBanDAO();
        this.lGiaBan = giabanDAO.getAll(config); 
        CNhomKhachHangDAO khdao = new CNhomKhachHangDAO();
        lNhom_kh = khdao.getAll(config);
        CTinhTrangDAO ttDAO = new CTinhTrangDAO();
        this.ltinh_trang = ttDAO.getAll(config);
        config.close();
        this.cbbSanPham.removeAllItems();
        for(CSanPham sp : listSanPham){
            this.cbbSanPham.addItem(sp.getTenSanPham());
        }
        
        this.cbbKhachHang.removeAllItems();
        this.cbbKhachHang.addItem("Khách lẻ");
        for(CKhachHang kh : lKhachHang){
            this.cbbKhachHang.addItem(kh.getHoTen());
            System.out.println("" + kh.getHoTen());
        }
        this.cbbKho.removeAllItems();
        for(CKho _kho : listKho){
            this.cbbKho.addItem(_kho.getTenKho());
        }
        
        this.cbbTinhTrangPhieu.removeAllItems();
        for(CTinhTrang item : ltinh_trang){
            this.cbbTinhTrangPhieu.addItem(item.getTen());
        }
        
        CModel _model = new CModel(QuanLySieuThi.user_name);
        
        this.lnhanvien = _model.getNhanVien();
        //su dung combobox cho nhan vien ban hang
        this.cbbnvbanhang.removeAllItems();
        for(CNhanVien nv : lnhanvien){
            this.cbbnvbanhang.addItem(nv.getHoTen());
        }
        
        this.loadDataHoaDon(_model);
    }
    
     private void loadDataHoaDon(CModel _model){
        this.lhoa_don = _model.getHoaDon(phieu);
        numberRow = lhoa_don.size();
        this.currentIndex = 0;
        this.view(currentIndex);
        this.setEditable(false);
     }
    
    private Boolean kiemTraNgay(){
        String strNgay = this.txtTongTien.getText();
        JOptionPane f = new JOptionPane("nhap ngay theo dinh dang  dd-mm-yyyy");
        return true;        
    }
     private Boolean editRow(){
        Date d = this.dtcNgayBan.getDate();
        
        if(d == null){
            d = new Date();
        }
        Timestamp t = new Timestamp(d.getYear(),d.getMonth(),d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds(),0);
        this.hdCurrent.setNgayNhap(t);
        CTinhTrang tinhtrang = this.ltinh_trang.get(this.cbbTinhTrangPhieu.getSelectedIndex());
        this.hdCurrent.setTinhTrang((int)tinhtrang.getId());
        d = new Date();
        t = new Timestamp(d.getYear(),d.getMonth(),d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds(),0);
        CModel _model = new CModel(QuanLySieuThi.user_name);
        Boolean flag = _model.UpdateHoaDon(hdCurrent, lcthd);        
        this.hdCurrent.setNgaySua(t);
        if(flag){
            JOptionPane.showMessageDialog(this, "Cap nhat thanh cong");
        }
        else{
            JOptionPane.showMessageDialog(this, "Cap nhat that bai");
        }
        return true;
    }
     
     private void view(int index){
         hdCurrent = null;
        if(index >= 0 && index < numberRow){
            CHoaDon item = this.lhoa_don.get(index);
            hdCurrent = item;
            id = item.getId();
            CModel _model = new CModel(QuanLySieuThi.user_name);
            lcthd = _model.getChiTiet(id);
            txtRows.setText(String.format("%s/%s",index + 1,numberRow));
            this.dtcNgayBan.setDate(item.getNgayNhap());
            txtTongTien.setText(df.format(item.getTongTien()));
            this.cbbKhachHang.setEnabled(true);
            int i = this.indexKhachHang(lKhachHang, item.getKhachHangId());
            this.cbbKhachHang.setSelectedIndex(i);            
            this.cbbKhachHang.setEnabled(false);
            int indexKho = _model.indexKho(listKho, item.getKhoId());
            this.cbbKho.setSelectedIndex(indexKho);
            i = _model.indexNhanVien(lnhanvien, item.getNguoilapId());
            if(i!= -1){
                CNhanVien nv = lnhanvien.get(i);
                this.txtNVNhap.setText(nv.getHoTen());
            }
            i = _model.indexNhanVien(lnhanvien, item.getNguoiGiaoId());
            if(i!= -1){                
                this.cbbnvbanhang.setSelectedIndex(i);
            }
            i = _model.indexTinhTrang(ltinh_trang, (long)item.getTinhTrang());
            if(i !=-1){
                this.cbbTinhTrangPhieu.setSelectedIndex(i);
            }
            this.LoadDataTable(lcthd);
        }
        else if(numberRow == 0){
            List<CChiTietHoaDon> lcthd = new ArrayList<CChiTietHoaDon>();
            this.LoadDataTable(lcthd);
            txtRows.setText(String.format("0/0"));
        }
    }
     
    private void LoadDataTable(List<CChiTietHoaDon> list){
        Vector<Vector> result = new Vector<Vector>();
        int stt = 1;
        for(CChiTietHoaDon item :list){
            Vector<String>row = new Vector<String>();
            row.addElement("" + stt);
            row.addElement("" + item.getTenSanPham());
            row.addElement(""+item.getSoLuong());
            row.addElement(df.format(item.getDongia()));
            row.addElement(df.format(item.getThanhTien()));
            result.addElement(row);
            stt++;
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("STT");
        columnNames.addElement("Tên SP");
        columnNames.addElement("Số lượng");
        columnNames.addElement("Đơn giá");
        columnNames.addElement("Thành tiền");
        DefaultTableModel model = new DefaultTableModel(result, columnNames);             
        this.tbDanhSach.removeAll();    
        this.tbDanhSach.setModel(model);        
        this.tbDanhSach.setFillsViewportHeight(true);
        this.tbDanhSach.setEnabled(true);      
    }
    
    private void setEditable(Boolean b){        
        this.txtTongTien.setEditable(b);
        this.cbbKhachHang.setEnabled(b);
        this.cbbKho.setEnabled(b);
        this.txtNVNhap.setEditable(b);
        this.cbbnvbanhang.setEnabled(b);
        this.cbbSanPham.setEditable(b);
        this.txtSoLuong.setEditable(b);
        this.txtvat.setEditable(b);
        this.txtDonGia.setEditable(b);       
    }
    private void clearText(){
        this.txtTongTien.setText("");
        this.cbbSanPham.setSelectedIndex(-1);
        this.txtdia_chi.setText("");
        this.txtNVNhap.setText("");        
        this.txtSoLuong.setText("1");
        this.txtvat.setText("10");
        this.txtDonGia.setText("");
        this.txtdien_thoai.setText("");
        this.txtThanhTien.setText("");
    }
    private Boolean isEmpty(){      
        if(this.txtTongTien.getText().equals("")){
            JOptionPane.showMessageDialog(this, String.format("Tổng Tiền chưa có dữ liệu"));
            return true;
        }
        if(this.txtNVNhap.getText().equals("")){
            JOptionPane.showMessageDialog(this, String.format("Vui lòng nhập vào ô Nhân viên nhập"));
            return true;
        }             
        if(this.txtSoLuong.getText().equals("")){
            JOptionPane.showMessageDialog(this, String.format("Vui lòng nhập vào ô Số lượng"));
            return true;
        }
        
        return false;
    }
    
    private void isSetEnabled(boolean b){        
        this.btnadd.setEnabled(b);
        this.btnSua.setEnabled(b);
        this.btnDelete.setEnabled(b);        
        this.btnSua.setEnabled(b);
        boolean flag = b == true ? false : true;
        this.btnAddChiTiet.setEnabled(flag);
        this.btnLuu.setEnabled(flag);
    }
    
  
    
    
    private Double tong_tien(List<CChiTietHoaDon> list){
        Double sum = 0.0;
        for(CChiTietHoaDon item : list){
            if(!item.getTang()){
                sum+= item.getTienTra();
            }
        }
        return sum;
    }
    
    private Boolean addNew(){
        if(listChiTiet.size() == 0){
            JOptionPane.showMessageDialog(this, "Vui lòng nhập ít nhất 1 mặt hàng cho hóa đơn.");
            return false;
        }
        long khachHangId = 1;
        int index = cbbKho.getSelectedIndex();
        long kho_id = 0;
        if(index >=0 && index < this.listKho.size()){
            kho_id = this.listKho.get(index).getId();
        }
        long nhan_vien_lap_id =  QuanLySieuThi.user_id;
        long nhan_vien_giao_id = QuanLySieuThi.user_id;
        index = cbbKhachHang.getSelectedIndex();
        if(index >=0 && index < this.lKhachHang.size()){
            khachHangId = lKhachHang.get(index).getId();
        }
        index = cbbnvbanhang.getSelectedIndex();
        if(index >=0 && this.lnhanvien.size() > index){
            nhan_vien_giao_id = this.lnhanvien.get(index).getId();
        }
        hoadon.setKhoId(kho_id);
        hoadon.setKhachHangId(khachHangId);
        hoadon.setLoaiPhieu(this.phieu);
        CTinhTrang ttCurrent = this.ltinh_trang.get(this.cbbTinhTrangPhieu.getSelectedIndex());
        hoadon.setTinhTrang((int)ttCurrent.getId());
        hoadon.setNguoiGiaoId(nhan_vien_giao_id);
        hoadon.setNguoiLapId(nhan_vien_lap_id); 
        hoadon.setNguoiTao(QuanLySieuThi.user_name);
        Date _nb = dtcNgayBan.getDate();
        Timestamp tNgayban = new Timestamp(_nb.getYear(),_nb.getMonth(),_nb.getDate(),_nb.getHours(),_nb.getMinutes(),_nb.getSeconds(),0);
        hoadon.setNgayNhap(tNgayban);
        Double tong_tien = Double.parseDouble(txtTongTien.getText().trim().replaceAll(",", ""));
        hoadon.setTongTien(tong_tien);
        CModel _model = new CModel(QuanLySieuThi.user_name);
       Boolean flag = _model.InsertHoaDon(hoadon, this.lShowcthdbh,null,lkho_xuat);
       int diem = 0;
       CConfig cf = null;// (CConfig)QuanLySieuThi.cfParams.get("donhangtangdiem");
       //donhangtangdiem
       if(QuanLySieuThi.cfParams != null){
           cf = (CConfig)QuanLySieuThi.cfParams.get("donhangtangdiem");
       }
       if(flag){
           if(cf != null){
               double ttCf = Double.parseDouble(cf.getValue().replaceAll(",", ""));
               if(ttCf >=tong_tien){
                   diem = Integer.parseInt(cf.getType());
               }
               _model.updateDiem(diem, khachHangId);               
            }
           JOptionPane.showMessageDialog(this, "Thêm mới thành công.");
       }
        loadDataHoaDon(_model);
        _model.Close();
        return true;
    }
    
    private void Save(){
        Boolean flag = false;
        if(this.add){
            flag = this.addNew();
        }
        else if(this.edit){
            flag = this.editRow();
        }
        if(flag){
            if(this._menu != null){
                this._menu.loadTonKho();
            }
            this.add = false;
            this.edit = false;
            this.isSetEnabled(true);
        }        
    }
    
    private void addChiTiet(){
        CChiTietHoaDon chitiet = new CChiTietHoaDon();
        long sanphamId = 1;
        CTon_Kho tkho = new CTon_Kho();
        int index = this.cbbSanPham.getSelectedIndex();    
        long san_pham_id = 0;
          String ten_san_pham = "";  
          if(index>=0 && index< listSanPham.size()){
              sanphamId = listSanPham.get(index).getId();
              sanphamCurrent = listSanPham.get(index);
              ten_san_pham = sanphamCurrent.getTenSanPham();
              san_pham_id = sanphamCurrent.getId();
          }
          
          int soluong = Integer.parseInt(this.txtSoLuong.getText().trim());
          Double don_gia = Double.parseDouble(this.txtDonGia.getText().trim().replaceAll(",", ""));
          index = this.cbbKho.getSelectedIndex();
          long kho_id = 0;
          if(index >=0 && index < listKho.size()){
              khoCurrent = listKho.get(index);
              kho_id = khoCurrent.getId();
          }
        tkho.setKhoId(khoCurrent.getId());
        tkho.setSanPhamId(sanphamId);
        CTon_Kho currentFind = this.tonkhoDAO.Find(tkho, listTonKho);
        if(currentFind!= null){
            tkho = currentFind;
        }
        int so_luong = Integer.parseInt(this.txtSoLuong.getText().trim());             
        Double thanh_tien = 0.0;       
        int slcon = tkho.getSoLuongNhap() - tkho.getSoLuongXuat();
        if(so_luong > slcon){
            JOptionPane.showMessageDialog(this, String.format("Trong cửa hàng chỉ còn lại %s không đủ hàng", slcon));                        
            return;
        } 
            chitiet.setSoLuong(so_luong);
            chitiet.setDongia(don_gia);
            chitiet.setHoaDonId(1);
            chitiet.setTenSanPham(ten_san_pham);
            chitiet.setSanPhamId(san_pham_id);
            thanh_tien = so_luong * don_gia;
            double gia_von = so_luong * this.don_gia_von;
            chitiet.setGiaVon(gia_von);
            thanh_tien = thanh_tien + thanh_tien * Double.parseDouble(txtvat.getText().trim())/100;               
            double tien_tra = thanh_tien - (thanh_tien * (chiet_khau/100));            
            chitiet.setThanhTien(thanh_tien);
            chitiet.setTenSanPham(ten_san_pham);
            chitiet.setTienTra(tien_tra);            
            this.listChiTiet.add(chitiet);
            lShowcthdbh.add(chitiet);
            this.setGiamGiaKhuyenMai(lShowcthdbh);
            this.setGiamGiaMuaNGiamM(lShowcthdbh);
            Double tong_tien = this.tong_tien(lShowcthdbh);
            this.txtTongTien.setText(df.format(tong_tien));
            getKhuyenMai(listChiTiet);// tang san pham khi mua san pham
            this.LoadDataTable(lShowcthdbh);            
            CTon_Kho k_xuat = new CTon_Kho();//kho xuat hang cho cua hang
            k_xuat.setKhoId(kho_id);
            k_xuat.setSanPhamId(san_pham_id);
            int _sl = 0;
            _sl = so_luong + currentFind.getSoLuongXuat();
            k_xuat.setSoLuongXuat(_sl);
            k_xuat.setSoLuongNhap(currentFind.getSoLuongNhap());
            this.lkho_xuat.add(k_xuat);
      //  }
        
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jDialog2 = new javax.swing.JDialog();
        btnLuu = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        btnadd = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lblNgayNhap = new javax.swing.JLabel();
        txtTongTien = new javax.swing.JTextField();
        lblNhanVienNhap = new javax.swing.JLabel();
        lblKhacHang = new javax.swing.JLabel();
        lblKhacHang1 = new javax.swing.JLabel();
        cbbKho = new javax.swing.JComboBox();
        cbbKhachHang = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        txtNVNhap = new javax.swing.JTextField();
        lblNhanVienNhap1 = new javax.swing.JLabel();
        dtcNgayBan = new com.toedter.calendar.JDateChooser();
        cbbnvbanhang = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtdia_chi = new javax.swing.JTextField();
        txtdien_thoai = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        cbbTinhTrangPhieu = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnPrev = new javax.swing.JButton();
        btnPrevBegin = new javax.swing.JButton();
        txtRows = new javax.swing.JTextField();
        btnNextEnd = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSach = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        cbbSanPham = new javax.swing.JComboBox();
        txtSoLuong = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtDonGia = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtvat = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtThanhTien = new javax.swing.JTextField();
        btnAddChiTiet = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtChiet_khau = new javax.swing.JTextField();
        btnPrint = new javax.swing.JButton();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jDialog2Layout = new javax.swing.GroupLayout(jDialog2.getContentPane());
        jDialog2.getContentPane().setLayout(jDialog2Layout);
        jDialog2Layout.setHorizontalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog2Layout.setVerticalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        btnLuu.setText("Lưu");
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        btnDelete.setText("Xóa");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnClose.setText("Thoát");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnadd.setText("Thêm mới");
        btnadd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddActionPerformed(evt);
            }
        });

        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(102, 102, 255));
        jLabel9.setText("LẬP HÓA ĐƠN BÁN HÀNG");

        jLabel10.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(102, 102, 255));
        jLabel10.setText("(Tại cửa hàng)");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin chung"));

        lblNgayNhap.setText("Ngày bán:");

        lblNhanVienNhap.setText("Tổng tiền:");

        lblKhacHang.setText("Khách hàng:");

        lblKhacHang1.setText("Cửa hàng:");

        cbbKho.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbbKhachHang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbKhachHang.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbKhachHangItemStateChanged(evt);
            }
        });

        jLabel8.setText("Nhân viên bán:");

        lblNhanVienNhap1.setText("Nhân viên lập:");

        dtcNgayBan.setDateFormatString("dd-MM-yyyy");

        cbbnvbanhang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Địa chỉ:");

        jLabel11.setText("Điện thoại:");

        cbbTinhTrangPhieu.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel12.setText("Tình trạng phiếu:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(lblNgayNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNhanVienNhap))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dtcNgayBan, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtdia_chi, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTongTien, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblKhacHang)
                        .addGap(28, 28, 28)
                        .addComponent(cbbKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(lblKhacHang1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbbTinhTrangPhieu, 0, 152, Short.MAX_VALUE)
                            .addComponent(cbbKho, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(0, 45, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(18, 18, 18)
                        .addComponent(cbbnvbanhang, 0, 161, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(lblNhanVienNhap1))
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNVNhap)
                            .addComponent(txtdien_thoai, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE))))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(cbbnvbanhang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtdien_thoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNhanVienNhap1)
                            .addComponent(txtNVNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNgayNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblKhacHang)
                                .addComponent(cbbKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(dtcNgayBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel12)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtdia_chi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(cbbTinhTrangPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblKhacHang1)
                            .addComponent(cbbKho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(3, 3, 3))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblNhanVienNhap)
                        .addComponent(txtTongTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách chi tiết sản phẩm"));

        btnPrev.setText("<");
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });

        btnPrevBegin.setText("<<");
        btnPrevBegin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevBeginActionPerformed(evt);
            }
        });

        btnNextEnd.setText(">>");
        btnNextEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextEndActionPerformed(evt);
            }
        });

        btnNext.setText(">");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        tbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbDanhSach);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(btnPrevBegin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnPrev)
                .addGap(18, 18, 18)
                .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnNextEnd)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 825, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 48, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPrev)
                    .addComponent(btnPrevBegin)
                    .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNext)
                    .addComponent(btnNextEnd))
                .addGap(18, 18, 18))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi tết sản phẩm"));

        jLabel4.setText("Tên sản phẩm:");

        cbbSanPham.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbSanPham.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbSanPhamItemStateChanged(evt);
            }
        });

        txtSoLuong.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSoLuongKeyReleased(evt);
            }
        });

        jLabel5.setText("Số lượng nhập:");

        txtDonGia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDonGiaActionPerformed(evt);
            }
        });
        txtDonGia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDonGiaKeyReleased(evt);
            }
        });

        jLabel1.setText("VAT (%):");

        jLabel6.setText("Đơn giá:");

        txtvat.setText("0");

        jLabel7.setText("Thành tiền:");

        txtThanhTien.setEditable(false);

        btnAddChiTiet.setText("Thêm chi tiết");
        btnAddChiTiet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddChiTietActionPerformed(evt);
            }
        });

        jLabel2.setText("Giảm giá % :");

        txtChiet_khau.setEditable(false);
        txtChiet_khau.setText("0");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(37, 37, 37)
                        .addComponent(cbbSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(55, 55, 55)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17)))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtvat, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2)))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(jLabel7))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(txtChiet_khau, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(32, 32, 32)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAddChiTiet, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(69, 69, 69))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(29, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(txtThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtvat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(cbbSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnAddChiTiet))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtChiet_khau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6))))
        );

        btnPrint.setText("In Hóa Đơn");
        btnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnadd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSua)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLuu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnClose)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPrint)))
                .addGap(205, 205, 205))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnadd)
                    .addComponent(btnSua)
                    .addComponent(btnLuu)
                    .addComponent(btnDelete)
                    .addComponent(btnClose)
                    .addComponent(btnPrint))
                .addGap(31, 31, 31))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txtDonGiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDonGiaActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_txtDonGiaActionPerformed

    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaddActionPerformed
        // TODO add your handling code here:
        
        tonkhoDAO = new CTonKhoDAO();
        hoadon = new CHoaDon();
        this.add = true;
        this.edit = false;
        
        this.isSetEnabled(false);
        setEditable(true);        
        listChiTiet = new ArrayList<CChiTietHoaDon>();
        lShowcthdbh = new ArrayList<CChiTietHoaDon>();
        this.LoadDataTable(listChiTiet);
        this.clearText();
        this.defaultValue();
        
    }//GEN-LAST:event_btnaddActionPerformed

    private void btnAddChiTietActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddChiTietActionPerformed
        // TODO add your handling code here:
        if(!this.add && !this.edit){
          JOptionPane.showConfirmDialog(this, "Vui long chon vao them moi hoac sua");
            return;
        }
        else
            this.addChiTiet();
    }//GEN-LAST:event_btnAddChiTietActionPerformed

    private CGiaBan getItemGiaBan(long san_pham_id){
        for(CGiaBan item : this.lGiaBan){
            if(item.getSanPhamId() == san_pham_id){
                return item;
            }
        }
        return null;
    }
    
    private void cbbSanPhamItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbSanPhamItemStateChanged
        // TODO add your handling code here:
        try{
            if(evt.getStateChange() == ItemEvent.SELECTED){
                int index = this.cbbSanPham.getSelectedIndex();
                if(index >=0 && index < this.listSanPham.size()){
                    sanphamCurrent = this.listSanPham.get(index);
                    CGiaBan gbCurrent = this.getItemGiaBan(sanphamCurrent.getId());
                    Double d_gia = 0.0;
                    
                    if(gbCurrent !=null){
                        d_gia = gbCurrent.getGiaBanKHVL();
                        this.don_gia_von = gbCurrent.getGiaNhap();
                    }
                    else{
                        d_gia = sanphamCurrent.getDonGia();
                        this.don_gia_von = sanphamCurrent.getGiaNhap();
                    }                    
                    txtvat.setText("" + sanphamCurrent.getThueVat());
                    txtDonGia.setText(df.format(d_gia));
                    if(!txtSoLuong.getText().equals("")){
                        int sl = Integer.parseInt(txtSoLuong.getText());
                        String value = txtDonGia.getText().replaceAll(",", "");
                        double don_gia = Double.parseDouble(value);
                        double thanh_tien = sl * don_gia;
                        txtThanhTien.setText(String.format("%s",df.format(thanh_tien)));
                    }     
                }
                //cuaHangCurr = 
            }
        }
        catch(Exception error){
            
        }
    }//GEN-LAST:event_cbbSanPhamItemStateChanged

    private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed
        // TODO add your handling code here:
        this.Save();
    }//GEN-LAST:event_btnLuuActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        // TODO add your handling code here:
        this.edit = true;
        this.add = false;
        this.isSetEnabled(false);
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btnCloseActionPerformed

    private void cbbKhachHangItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbKhachHangItemStateChanged
        // TODO add your handling code here:
        try{
            if(evt.getStateChange() == ItemEvent.SELECTED){
                khCurrent = new CKhachHang();
                khCurrent.setNhomKhId(1);
                int index = this.cbbKhachHang.getSelectedIndex();
                chiet_khau =0;
                //lKhachHang
                if(index  >=1 && index <= this.lKhachHang.size()){
                    int t = index - 1;
                    khCurrent = lKhachHang.get(t);                    
                    for(CNhomKhachHang item : lNhom_kh){
                        if(item.getId() == khCurrent.getNhomKhId()){
                            chiet_khau = item.getChietKhau();
                            break;
                        }
                    }
                    this.txtChiet_khau.setText(String.format("%s", this.chiet_khau));
                    this.txtdien_thoai.setText(khCurrent.getDienThoai());
                    txtdia_chi.setText(khCurrent.getDiaChi());
                    txtdia_chi.setEditable(false);
                    txtdien_thoai.setEditable(false);
                }
                else{
                    if(this.add || this.edit){
                        txtdia_chi.setEditable(true);
                        txtdien_thoai.setEditable(true);
                    }
                    else{
                        txtdia_chi.setEditable(false);
                        txtdien_thoai.setEditable(false);
                    }
                }
            }            
        }
        catch(Exception error){
            
        }
    }//GEN-LAST:event_cbbKhachHangItemStateChanged

    private void txtSoLuongKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSoLuongKeyReleased
        // TODO add your handling code here:
          try{
            if(!txtSoLuong.getText().equals("")){
                int sl = Integer.parseInt(txtSoLuong.getText());
                String value = txtDonGia.getText().replaceAll(",", "");
                double don_gia = Double.parseDouble(value);
                double thanh_tien = sl * don_gia;
                txtThanhTien.setText(String.format("%s",df.format(thanh_tien)));
            }            
        } catch(Exception error){
            
        }
    }//GEN-LAST:event_txtSoLuongKeyReleased

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        // TODO add your handling code here:
        if(this.currentIndex > 0){
            this.currentIndex--;
            this.view(this.currentIndex);
        }
    }//GEN-LAST:event_btnPrevActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        if(this.currentIndex < this.numberRow){
            this.currentIndex++;
            this.view(this.currentIndex);
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnNextEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextEndActionPerformed
        // TODO add your handling code here:
         this.currentIndex = this.numberRow - 1;
         this.view(this.currentIndex);
    }//GEN-LAST:event_btnNextEndActionPerformed

    private void btnPrevBeginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevBeginActionPerformed
        // TODO add your handling code here:
         this.currentIndex = 0;
         this.view(this.currentIndex);
    }//GEN-LAST:event_btnPrevBeginActionPerformed

    private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintActionPerformed
        // TODO add your handling code here:
        String fileName = QuanLySieuThi.pathFolderReport + "chitiethoadon.jrxml" ;        
        HashMap parameters = new HashMap();
        //parameters.put("title", title);
       parameters.put("p_tench",this.cbbKho.getSelectedItem().toString());
       parameters.put("p_tenkh",this.cbbKhachHang.getSelectedItem().toString());
       parameters.put("p_khuyenmai","");
       parameters.put("p_sohd","");
       Date d = this.dtcNgayBan.getDate();
       int year = d.getYear() + 1900;
       String ngayban = d.getDate() + "-" + d.getMonth() + "-" + year ;
       parameters.put("p_ngayban",ngayban);       
       parameters.put("p_nvban",this.cbbnvbanhang.getSelectedItem().toString());
       parameters.put("p_nvlap",this.txtNVNhap.getText());
       JRBeanCollectionDataSource collectionDataSource = new JRBeanCollectionDataSource(this.lcthd);
       FrmReport f = null;// new FrmReport(begin, end, kho, loai_phieu, fileName, title,theo);
       f = new FrmReport(collectionDataSource, fileName, parameters);
       f.setVisible(true);
    }//GEN-LAST:event_btnPrintActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if(this.hdCurrent != null){
            int repley  = JOptionPane.showConfirmDialog(this, "Ban co muon xoa hoa don nay khong?");
            if(repley == JOptionPane.YES_OPTION){
                CModel _model = new CModel(QuanLySieuThi.user_name);
                Boolean flag = _model.deleteHoaDon(hdCurrent);
                if(flag){
                    JOptionPane.showMessageDialog(this, "Xoa thanh cong");
                    this.currentIndex = 0;
                    this.loadDataHoaDon(_model);     
                }
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void txtDonGiaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDonGiaKeyReleased
        // TODO add your handling code here:
         // TODO add your handling code here:
          try{
            if(!txtSoLuong.getText().equals("")){
                int sl = Integer.parseInt(txtSoLuong.getText());
                String value = txtDonGia.getText().replaceAll(",", "");
                double don_gia = Double.parseDouble(value);
                double thanh_tien = sl * don_gia;
                txtThanhTien.setText(String.format("%s",df.format(thanh_tien)));
            }            
        } catch(Exception error){
            
        }
    }//GEN-LAST:event_txtDonGiaKeyReleased

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddChiTiet;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnNextEnd;
    private javax.swing.JButton btnPrev;
    private javax.swing.JButton btnPrevBegin;
    private javax.swing.JButton btnPrint;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnadd;
    private javax.swing.JComboBox cbbKhachHang;
    private javax.swing.JComboBox cbbKho;
    private javax.swing.JComboBox cbbSanPham;
    private javax.swing.JComboBox cbbTinhTrangPhieu;
    private javax.swing.JComboBox cbbnvbanhang;
    private com.toedter.calendar.JDateChooser dtcNgayBan;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblKhacHang;
    private javax.swing.JLabel lblKhacHang1;
    private javax.swing.JLabel lblNgayNhap;
    private javax.swing.JLabel lblNhanVienNhap;
    private javax.swing.JLabel lblNhanVienNhap1;
    private javax.swing.JTable tbDanhSach;
    private javax.swing.JTextField txtChiet_khau;
    private javax.swing.JTextField txtDonGia;
    private javax.swing.JTextField txtNVNhap;
    private javax.swing.JTextField txtRows;
    private javax.swing.JTextField txtSoLuong;
    private javax.swing.JTextField txtThanhTien;
    private javax.swing.JTextField txtTongTien;
    private javax.swing.JTextField txtdia_chi;
    private javax.swing.JTextField txtdien_thoai;
    private javax.swing.JTextField txtvat;
    // End of variables declaration//GEN-END:variables
}
